package com.weixin.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.weixin.entity.User;
import com.weixin.service.StudentService;
import com.weixin.service.impl.impl;


@WebServlet("/UserServlet/*")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	impl service=new impl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getPathInfo().substring(1);
		if ("login".equals(path)) {
			selectUser(request,response);
		}else if("ListByRole".equals(path)){
			ListByRole(request,response);
		}else if("remove".equals(path)) {
			remove(request,response);
		}else if("removeAll".equals(path)) {
			removeAll(request,response);
		}else if("pageRole".equals(path)) {
			pageRole(request,response);
		}else if("AllSearch".equals(path)) {
			AllSearch(request,response);
		}
	}

	private void AllSearch(HttpServletRequest request, HttpServletResponse response) {
		int pageSize = Integer.valueOf(request.getParameter("pageSize"));
	      int pageNum = Integer.valueOf(request.getParameter("pageNum"));
	      String role = request.getParameter("role");
	      String search = request.getParameter("search");
	      try {
			List<User> selectAllSearch = service.selectAllSearch(role, search, pageNum, pageSize);
			double pageCount = Math.ceil(service.selectAllTotalSearch(role, search)*1.0/pageSize);
			 Map<String, Object> map=new HashMap<>();
			  map.put("userList", selectAllSearch);
			  map.put("pageCount", pageCount);
			  map.put("pageNum",pageNum);
			  response.getWriter().print(new Gson().toJson(map));
	      } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void pageRole(HttpServletRequest request, HttpServletResponse response) {
		      int pageSize = Integer.valueOf(request.getParameter("pageSize"));
		      int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		      String role = request.getParameter("role");
		      try {
				List<User> listAllBypage = service.ListAllBypage(role, pageNum, pageSize);
				//计算总记录数
				double count = Math.ceil((service.getTotal(role)*1.0)/pageSize);
			  Map<String, Object> map=new HashMap<>();
			  map.put("userList", listAllBypage);
			  map.put("pageCount", count);
			  map.put("pageNum",pageNum);
			  response.getWriter().print(new Gson().toJson(map));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}

	private void removeAll(HttpServletRequest request, HttpServletResponse response) {
		String u_ids = request.getParameter("u_ids");
		List json = new Gson().fromJson(u_ids, List.class);
		try {
			if (service.deleteByAll(json)) {
				response.getWriter().print("1");
			}else {
				response.getWriter().print("0");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

	private void remove(HttpServletRequest request, HttpServletResponse response) {
		String u_id = request.getParameter("u_id");
		try {
			if (service.removeByid(Integer.valueOf(u_id))) {
				response.getWriter().print("1");
			}else {
				response.getWriter().print("0");
			}
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void ListByRole(HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
		String role = request.getParameter("role");
		try {
			List<User> userList = service.ListByrole(role);
			Gson gson = new Gson();
			String userListJson = gson.toJson(userList);
			response.getWriter().print(userListJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void selectUser(HttpServletRequest request, HttpServletResponse response) {
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		try {
			User user=null;
			if ((user=service.select(name, password))!=null) {
				request.getSession().setAttribute("user", user);
				response.sendRedirect(request.getContextPath()+"/index.jsp");
			}else {
				request.setAttribute("error", "用户名或密码错误");
				request.getRequestDispatcher("/login.jsp").forward(request, response);	
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
